package io.bba.boot.app.controller;

import io.bba.boot.auth.annotation.Permission;
import io.bba.boot.common.enums.SysLogType;
import io.bba.boot.framework.annotation.Log;
import io.bba.boot.framework.page.Paging;
import io.bba.boot.framework.response.ApiResult;
import io.bba.boot.app.dto.ArticleDto;
import io.bba.boot.app.query.ArticleQuery;
import io.bba.boot.app.service.ArticleService;
import io.bba.boot.app.vo.ArticleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 文章 控制器
 *
 * @author zs
 * @since 2024-03-22
 */
@Slf4j
@RestController
@Tag(name = "文章")
@RequestMapping("/admin/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.ADD)
    @Operation(summary = "添加文章")
    @PostMapping("/addArticle")
    @Permission("article:add")
    public ApiResult addArticle(@Valid @RequestBody ArticleDto dto) {
        log.info("添加文章：{}", dto);
        boolean flag = articleService.addArticle(dto);
        return ApiResult.result(flag);
    }

    /**
     * 修改文章
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.UPDATE)
    @Operation(summary = "修改文章")
    @PostMapping("/updateArticle")
    @Permission("article:update")
    public ApiResult updateArticle(@Valid @RequestBody ArticleDto dto) {
        log.info("修改文章：{}", dto);
        boolean flag = articleService.updateArticle(dto);
        return ApiResult.result(flag);
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.DELETE)
    @Operation(summary = "删除文章")
    @PostMapping("/deleteArticle/{id}")
    @Permission("article:delete")
    public ApiResult deleteArticle(@PathVariable Long id) {
        log.info("删除文章：{}", id);
        boolean flag = articleService.deleteArticle(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取文章详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取文章详情")
    @PostMapping("/getArticle/{id}")
    @Permission("article:info")
    public ApiResult<ArticleVo> getArticle(@PathVariable Long id) {
        log.info("获取文章详情：{}", id);
        ArticleVo articleVo = articleService.getArticleById(id);
        return ApiResult.success(articleVo);
    }

    /**
     * 获取文章分页列表
     *
     * @param query
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取文章分页列表")
    @PostMapping("/getArticlePage")
    @Permission("article:page")
    public ApiResult<ArticleVo> getArticlePage(@Valid @RequestBody ArticleQuery query) {
        log.info("获取文章分页列表：{}", query);
        Paging<ArticleVo> paging = articleService.getArticlePage(query);
        return ApiResult.success(paging);
    }

}
